package xyz.labmem.base.sys.module.dict.controller

import cn.hutool.core.bean.BeanUtil
import xyz.labmem.base.sys.base.BaseController
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import xyz.labmem.base.core.result.RP
import xyz.labmem.base.core.result.RV
import xyz.labmem.base.core.base.BaseVerify
import xyz.labmem.base.core.tool.isNotBlank
import xyz.labmem.base.sys.consts.PathPrefixConst
import xyz.labmem.base.sys.module.dict.controller.request.*
import xyz.labmem.base.sys.module.dict.entity.SysDictData
import xyz.labmem.base.sys.module.dict.entity.SysDictType
import xyz.labmem.base.sys.module.dict.mapper.SysDictDataMapper
import xyz.labmem.base.sys.module.dict.mapper.SysDictTypeMapper

/**
 * @Author lty
 * @Date 2021/12/16 15:56
 */
@Api(tags = ["字典管理"])
@RestController
@RequestMapping("${PathPrefixConst.SYS_PATH_PREFIX}dict")
class SysDictController(
    private var sysDictTypeMapper: SysDictTypeMapper,
    private var sysDictDataMapper: SysDictDataMapper,
) : BaseController() {

    @PostMapping("getTypePage")
    @ApiOperation(value = "获取字典类型分页")
    fun typePage(
        @RequestBody @Validated(BaseVerify.page::class) req: GetDictTypePageReq
    ): RP<List<SysDictType>> {
        return page(
            sysDictTypeMapper.selectPage(
                req.IPage(),
                KtQueryWrapper(SysDictType::class.java)
                    .like(req.name.isNotBlank(), SysDictType::name, req.name)
                    .orderByAsc(SysDictType::sort)
            )
        )
    }

    @PostMapping("getDataPage")
    @ApiOperation(value = "获取字典类型值的分页")
    fun getDataPage(
        @RequestBody @Validated(BaseVerify.page::class) req: GetDictDataPageReq
    ): RP<List<SysDictData>> {
        return page(
            sysDictDataMapper.selectPage(
                req.IPage(),
                KtQueryWrapper(SysDictData::class.java)
                    .eq(SysDictData::typeId, req.typeId)
                    .like(req.code.isNotBlank(), SysDictData::code, req.code)
                    .like(req.value.isNotBlank(), SysDictData::value, req.value)
                    .orderByAsc(SysDictData::sort)
            )
        )
    }

    @PutMapping("createType")
    @ApiOperation(value = "新建字典类型")
    fun createType(
        @RequestBody @Validated(BaseVerify.put::class) req: CreateDictTypeReq
    ): RV<*> {
        val count = sysDictTypeMapper.selectCount(
            KtQueryWrapper(SysDictType::class.java)
                .eq(SysDictType::code, req.code)
        ).toInt()
        return if (count == 0) {
            val dt = SysDictType()
            BeanUtil.copyProperties(req, dt)
            resultJudgment(dt.insert())
        } else
            failedMsg("字典类型code已存在！")
    }

    @PutMapping("addData")
    @ApiOperation(value = "字典类型添加值")
    fun addData(
        @RequestBody @Validated(BaseVerify.put::class) req: CreateDictDataReq
    ): RV<*> {
        val type = sysDictTypeMapper.selectById(req.typeId)
        return if (type != null) {
            val dd = SysDictData()
            BeanUtil.copyProperties(req, dd)
            resultJudgment(dd.insert())
        } else
            failedMsg("字典类型不存在")
    }

    @PatchMapping("updateType")
    @ApiOperation(value = "修改字典类型")
    fun updateType(
        @RequestBody @Validated(BaseVerify.update::class) req: UpdateDictTypeReq
    ): RV<*> {
        val type = sysDictTypeMapper.selectById(req.id)
        if (type != null) {
            if (type.code != req.code) {
                val count = sysDictTypeMapper.selectCount(
                    KtQueryWrapper(SysDictType::class.java)
                        .eq(SysDictType::code, req.code)
                ).toInt()
                if (count > 0)
                    return failedMsg("字典类型code已存在！")
            }
            BeanUtil.copyProperties(req, type)
            return resultJudgment(type.updateById())
        } else
            return failedMsg("要修改的字典类型不存在")
    }

    @PatchMapping("updateData")
    @ApiOperation(value = "修改字典值")
    fun updateData(
        @RequestBody @Validated(BaseVerify.update::class) req: UpdateDictDataReq
    ): RV<*> {
        val data = sysDictDataMapper.selectById(req.id)
        return if (data != null) {
            BeanUtil.copyProperties(req, data)
            data.typeId = null
            resultJudgment(data.updateById())
        } else
            failedMsg("字典类型不存在")
    }

}